/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import java.util.Properties;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.inject.Named;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Ahmed
 */
@Named(value = "sender")
@RequestScoped
public class MailSender {
@Resource(lookup = "MAILCONFIG")
    private Properties mailProp;
    @Inject
    Configs c;
    private String subject;
    private String recipnt;
    private String content;
    private String output;
    private boolean done=false;

    /**
     * Creates a new instance of MailSender
     */
    public MailSender() {

    }

    public void sendMail(ActionEvent event) {

        String result;

        // Assuming you are sending email from localhost
        // Get system properties object
        Properties properties = System.getProperties();

        // Setup mail server
        System.out.println("setting properties");
        properties.setProperty("mail.smtp.host", c.getHost());
        properties.setProperty("mail.smtp.port", c.getPort());
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.smtp.starttls.enable", "true");

        // Get the default Session object.
        System.out.println("authinticating");
        Authenticator auth = new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                PasswordAuthentication pa = new PasswordAuthentication(c.getId(), c.getPassword());
                return pa;
            }
        };
        System.out.println("id="+c.getId()+" and pass="+mailProp.getProperty("password"));
        Session mailSession = Session.getInstance(properties, auth);
        System.out.println("creating sesion");
        try {
            // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(mailSession);
            // Set From: header field of the header.
            message.setFrom(new InternetAddress(c.getId()));
            // Set To: header field of the header.
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(recipnt));
            // Set Subject: header field
            message.setSubject(subject);
            // Now set the actual message
            message.setText(content);
            // Send message
            System.out.println("sending message");
            Transport.send(message);
            result = "Sent message successfully....";
            done=true;
            FacesContext context = FacesContext.getCurrentInstance(); 
            context.addMessage(null, new FacesMessage("Successful", result)); 
        } catch (MessagingException mex) {
            result = "Error: unable to send message....";
        }

        output = result;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getRecipnt() {
        return recipnt;
    }

    public void setRecipnt(String recipnt) {
        this.recipnt = recipnt;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getOutput() {
        return output;
    }

    public void setOutput(String output) {
        this.output = output;
    }

    /**
     * @return the done
     */
    public boolean isDone() {
        return done;
    }

    /**
     * @param done the done to set
     */
    public void setDone(boolean done) {
        this.done = done;
    }
}
